Thread: Word counting [1.5.4 The C programming language book ]

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    12

    Word counting [1.5.4 The C programming language book ]

    I have just started learning programming myself referring "The C Programming Language by Brian Kernighan and Dennis Ritchie". In the following program

    Code:
    #include <stdio.h>
    
    
    int main ()
    {
        int c , n , state , nc , nw , nl ;
        state = 0 ;
        nc = nw = nl = 0 ;
        while ((c = getchar()) != EOF)
        {
            ++nc ;
            if (c == '\n')
                ++nl ;
            if (c == ' ' || c == '\n')
                state = 0 ;
            else if (state == 0 )
            {
                state = 1 ;
                ++nw ;
            }
            printf("%2d%2d%2d%2d\n" , state ,nc , nw , nl ) ;
        }
        }
    i modified a bit by putting printf() in the loop but i cant understand how its working as getchar() reads character by character when it encounters "abc" after reading 'a' the state should be changed to 1 and it would not read b and c .
    I may be wrong but that what I understood . Sorry if the question is pretty basic but i don't wanna move forward with broken concepts.

    Thanks in advance

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The read operation happens regardless of what value the state variable has, it runs until EOF is achieved.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    12
    But the value of the state variable will be one , how it will read the upcoming characters?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by LearnerForever View Post
    But the value of the state variable will be one , how it will read the upcoming characters?
    why do you think state variable value will interfere with reading character?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    12
    Thanks for the replies I understood the concept , it is counting word only.

    Thanks for the replies

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The C Programming language (K&R) Book
    By kidharris55 in forum General Discussions
    Replies: 14
    Last Post: 09-11-2014, 04:45 AM
  2. The C Programming language (K&R) Book
    By kidharris55 in forum C Programming
    Replies: 3
    Last Post: 09-08-2014, 07:37 PM
  3. binsearch function c programming language book
    By blob84 in forum C Programming
    Replies: 4
    Last Post: 05-17-2011, 04:36 AM
  4. a mistake in the c programming language book.wtf?
    By KidMan in forum C Programming
    Replies: 9
    Last Post: 01-17-2006, 04:33 AM
  5. The C++ Programming Language(book) exercise troubles
    By Daniel Primed in forum C++ Programming
    Replies: 11
    Last Post: 11-07-2005, 01:56 AM